/**
 * This file is part of "Subversion Server Configuration (win32)".

    "Subversion Server Configuration (win32)" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Subversion Server Configuration (win32)" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Subversion Server Configuration (win32)".  If not, see <http://www.gnu.org/licenses/>.
 * 
 *
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace Subversion_Configuration_Win32
{
    public class SvnRepo
    {
        private String[] readonly_users;
        private String[] readonly_groups;
                
        private String[] full_users;
        private String[] full_groups;

        private string name;

        //nome del repo svn
        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        public String[] FullAccess_Users
        {
            get { return full_users; }
            set { full_users = value; }
        }

        public String[] ReadOnly_Users
        {
            get { return readonly_users; }
            set { readonly_users = value; }
        }


        public String[] ReadOnly_Groups
        {
            get { return readonly_groups; }
            set { readonly_groups = value; }
        }

        public String[] FullAccess_Groups
        {
            get { return full_groups; }
            set { full_groups = value; }
        }

        public SvnRepo(String name)
        {
            Name = name;
        }


        //delete groups from read-only which are with full access
        internal void sanitizeGroups()
        {
            if (this.readonly_groups != null)
            {
                List<String> tempListReadOnly = new List<string>();
                foreach (String group in this.readonly_groups)
                {
                    if (!isInGroup(this.full_groups, group))
                    {
                        tempListReadOnly.Add(group);
                    }
                }

                int count = tempListReadOnly.Count;
                String[] tempReadOnlyGroups = new String[count];

                for (int i = 0; i < count; i++)
                {
                    tempReadOnlyGroups[i] = tempListReadOnly[i];
                }

                this.readonly_groups = tempReadOnlyGroups;
            }
        }

        private bool isInGroup(string[] p, string elem)
        {
            if (p != null)
            {
                foreach (String item in p)
                {
                    if (item.Equals(elem))
                        return true;
                }
            }
            return false;            
        }

        //delete users from read-only which are with full access
        internal void sanitizeUsers()
        {
            if (this.readonly_users != null)
            {
                List<String> tempListReadOnly = new List<string>();

                foreach (String user in this.readonly_users)
                {
                    if (!isInGroup(this.full_users, user))
                    {
                        tempListReadOnly.Add(user);
                    }
                }

                int count = tempListReadOnly.Count;
                String[] tempReadOnlyUsers = new String[count];

                for (int i = 0; i < count; i++)
                {
                    tempReadOnlyUsers[i] = tempListReadOnly[i];
                }

                this.readonly_users = tempReadOnlyUsers;
            }
        }
    }
}
